package com.example.lastwork.controller;


import com.example.lastwork.common.controller.LogicController;
import com.example.lastwork.dao.LineItemDao;
import com.example.lastwork.dto.LineItemDTO;
import com.example.lastwork.dto.RestDTO;
import com.example.lastwork.entity.LineItem;
import com.example.lastwork.service.LineItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Api(tags = "订单项服务接口")
@RestController
@RequestMapping("/LineItemController")
public class LineItemController extends LogicController<LineItemService, LineItemDao, LineItem, Long> {
    /** DTO转化器 */
    @Resource
    private ModelMapper modelMapper;

    public LineItemController(@Autowired LineItemService service){
        super(service);
    }

    @ApiOperation("根据订单编号查询商品信息")
    @RequestMapping(value = "/getLineItemsByOrdersId",method = RequestMethod.POST)
    public RestDTO<List<LineItemDTO>> getLineItemsByOrdersId(@ApiParam("订单编号") Long ordersId){
        List<LineItem> lineItemList = getService().getLineItemsByOrdersId(ordersId);
        return RestDTO.success(lineItemList.stream()
                .map(lineItem -> modelMapper.map(lineItem, LineItemDTO.class))
                .collect(Collectors.toList()));
    }
}
